package com.myhouse.controller;

import javax.annotation.Resource;
import javax.imageio.spi.RegisterableService;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myhouse.biz.UserService;
import com.myhouse.entity.User;

@Controller
public class AccountController {
	
	@Resource
	private UserService userService;
	
	@RequestMapping("/login")
	public String login() {
		return "login";
	}
	
	@RequestMapping("/register")
	public String RegisterableService() {
		return "register";
	}

	@RequestMapping("/checkLogin")
	@ResponseBody
	public String checkLogin(String username,String password,HttpSession session) {
		User user = userService.checkLogin(username, password);
		if(user!=null){
			if(user.getIsInUse()==true) {
				return "0";
			}else {
				System.out.println(user.getIsInUse());
				session.setAttribute("user",user);
				return "1";
			}
		}
		return "2";
	}
	
	@RequestMapping("/userRemove")
	public String userRemove(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:index";
	}
	
	@RequestMapping("/checkUsername")
	@ResponseBody
	public String checkUsername(String username) {
		User user = userService.checkUsername(username);
		if(user==null)
			return "1";
		return null;
	}
	
	@RequestMapping("/doRegister")
	public String doRegister(User user) {
		userService.addUser(user);
		return "redirect:login";
	}
}
